package org.fnzn.service.Impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.fnzn.bean.LrdUser;
import org.fnzn.bean.SysLog;
import org.fnzn.dao.SysLogMapper;
import org.fnzn.service.SysLogServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.fnzn.core.Page;
import org.fnzn.core.Result;
import org.fnzn.dao.LrdOrgMapper;
import org.fnzn.dao.LrdUserMapper;
import org.fnzn.service.base.AbstractService;
import org.fnzn.util.ResultGenerator;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;


@Service
public class SysLogServiceImpl extends AbstractService<SysLog> implements SysLogServcie {
	
	@Autowired
    SysLogMapper sysLogMapper;
	@Autowired
	private LrdOrgMapper lrdOrgMapper;
	@Autowired
	private LrdUserMapper lrdUserMapper;
	
	@Override
	public Result findSysLog(SysLog sysLog,Page page) {
		// TODO Auto-generated method stub
			if(page.getLimit()!=null&& page.getPage()!=0){
				Condition condition = new Condition(SysLog.class);
				Criteria criteria=condition.createCriteria();
				if(StringUtils.isNotBlank(sysLog.getCrtDate())) {
					criteria.andCondition("OPER_DATE >=", (sysLog.getCrtDate()+"").replace("/", ""));
				}
				if(StringUtils.isNotBlank(sysLog.getMtnDate())) {
					criteria.andCondition("OPER_DATE <=", (sysLog.getMtnDate()+"").replace("/", ""));
				}
				if(StringUtils.isNotBlank(sysLog.getOrgCd())){
					criteria.andEqualTo("orgCd",sysLog.getOrgCd());
				}
				if(StringUtils.isNotBlank(sysLog.getUserId())){
					String[] userIDs = sysLog.getUserId().toString().split(",");
					List<String> userIds=Arrays.asList(userIDs);
					criteria.andIn("userId", userIds);
				}
				PageHelper.startPage(page.getPage(),page.getLimit(), true);
				List<SysLog> list = findByCondition(condition);
				page.setRows(list);
				page.setTotal(new PageInfo(list).getTotal());
				return ResultGenerator.genSuccessResult(page);
			}
			return ResultGenerator.genFailResult("查询失败");
	}

	@Override
	public Result findByPK(Map<String,Object> map) {
		// TODO Auto-generated method stub
		List<LrdUser> listLrdUser =lrdUserMapper.selectAll();
		List<Map<String,Object>> listLrdOrg = lrdOrgMapper.findMenu(map);
		Map<String,Object> resultmap=new HashMap<String, Object>();
		resultmap.put("org", listLrdOrg);
		resultmap.put("user", listLrdUser);
		return ResultGenerator.genSuccessResult(resultmap);
	}

	@Override
	public Result deleteSysLog() {
		// TODO Auto-generated method stub
		sysLogMapper.deleteAll();
		return ResultGenerator.genSuccessResult();
	}
}
